//
//  ShopingViewController.m
//  ZCooking
//
//  Created by 韩旭 on 16/8/27.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "ShopingViewController.h"
#import "HX_Macro.h"
#import "HXShopingCCell.h"
#import "HXHotWaresCCell.h"
#import "HXCycleCCell.h"
#import "HXCycleImageCell.h"
#import "HXTopBannerModel.h"
#import "HXScrollBannerModel.h"
#import "HXHotShopingModel.h"
#import "HXTopicsModel.h"
#import "HXShopingTopVC.h"
#define kShoping @"reuseShoping"
#define kCycleView @"reuseCycle"
#define kHotWares @"reuseHotWares"
@interface ShopingViewController ()< UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) UILabel *labelTitle;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *topBanner;
@property (nonatomic, strong) NSMutableArray *scrollBanner;
@property (nonatomic, strong) NSMutableArray *arrShoping;
@property (nonatomic, copy) NSString *counts;
@property (nonatomic, assign) NSInteger page;
@end

@implementation ShopingViewController
- (NSMutableArray *)arrShoping {
    if (!_arrShoping) {
        _arrShoping = [NSMutableArray array];
    }
    return _arrShoping;
}
- (NSMutableArray *)topBanner {
    if (!_topBanner) {
        _topBanner = [NSMutableArray array];
    }
    return _topBanner;
}
- (NSMutableArray *)scrollBanner {
    if (!_scrollBanner) {
        _scrollBanner = [NSMutableArray array];
    }
    return _scrollBanner;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.titleView = [[UIView alloc] init];
    _titleView.backgroundColor = [UIColor whiteColor];
    _titleView.HX_width = SCREEN_WIDTH;
    _titleView.HX_height = 49;
    _titleView.HX_y = 0;
    _titleView.HX_x = 0;
    self.page = 1;
    [self.view addSubview:_titleView];
    self.labelTitle = [[UILabel alloc] init];
    _labelTitle.text = @"商城";
    _labelTitle.HX_width = [SelfSizeTools selfSizeHeightWithString:_labelTitle.text height:50 Font:[UIFont systemFontOfSize:17]];
    _labelTitle.centerX = _titleView.centerX;
    _labelTitle.HX_height = 49;
    [self.titleView addSubview:_labelTitle];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = COLORAUTO;
    [self getNetWorkingModel];
    [self getNewWorkingShoping];
    [self creatCollectionView];
}
- (void)getNewWorkingShoping {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = @"methodName=MallHomepageList&version=4.3&page=1&size=10&_time=1474439960&_signature=21e725800bb35fee0e1ce9428c773bfd";
    NSDictionary *dicHeader = @{@"Host" : @"api.izhangchu.com", @"Rferer" : @"http://h5.izhangchu.com/market/index.html?token=88E92FD7679C76FA91A54FE14641AD60&user_id=1666223&from=app&", @"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *arrData = dicData[@"data"];
        self.counts = dicData[@"total"];
        for (NSDictionary *dicAll in arrData) {
            [self.arrShoping addObject:[HXHotShopingModel ModelWithDic:dicAll]];
        }
        [_collectionView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (void)getNetWorkingModel {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = @"methodName=MallHomepage&version=4.3&_time=1474439960&_signature=21e725800bb35fee0e1ce9428c773bfd";
    NSDictionary *dicHeader = @{@"Host" : @"api.izhangchu.com", @"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8", @"Referer" : @"http://h5.izhangchu.com/market/index.html?token=88E92FD7679C76FA91A54FE14641AD60&user_id=1666223&from=app&"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *topBanner = dicData[@"topBanner"];
        for (NSDictionary *dicTop in topBanner) {
            HXTopBannerModel *model = [HXTopBannerModel ModelWithDic:dicTop];
            [self.topBanner addObject:model];
        }
        NSArray *scrollBanner = dicData[@"scrollBanner"];
        for (NSDictionary *dicScroll in scrollBanner) {
            [self.scrollBanner addObject:    [HXScrollBannerModel ModelWithDic:dicScroll]];
        }
        [_collectionView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (void)creatCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 49, SCREEN_WIDTH, SCREEN_HEIGHT - 93) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1.00];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    [_collectionView registerNib:[UINib nibWithNibName:@"HXShopingCCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kShoping];
    [_collectionView registerClass:[HXCycleCCell class] forCellWithReuseIdentifier:kCycleView];
    [_collectionView registerNib:[UINib nibWithNibName:@"HXHotWaresCCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kHotWares];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    layout.sectionHeadersPinToVisibleBounds = NO;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return _topBanner.count;
    } else if (section == 1) {
        return 1;
    } else {
        return self.arrShoping.count;
    }
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        reusable.backgroundColor = [UIColor colorWithWhite:0.979 alpha:1.000];
        UILabel *label = [[UILabel alloc] init];
        label.text = @"热门商品";
        label.HX_width = [SelfSizeTools selfSizeHeightWithString:label.text height:50 Font:[UIFont systemFontOfSize:17]];
        label.textColor = [UIColor colorWithWhite:0.322 alpha:1.000];
        label.font = [UIFont systemFontOfSize:15];
        label.HX_height = 50;
        label.HX_x = 20;
        [reusable addSubview:label];
        return reusable;
    } else {
        UICollectionReusableView *foot = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(10, 10, SCREEN_WIDTH - 20, 30);
        NSInteger count = [self.counts integerValue];
        [btn setTitle:[NSString stringWithFormat:@"还有下%ld个商品", count - self.page * 10] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 10;
        [btn addTarget:self action:@selector(addBtn:) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = [UIColor lightGrayColor];
        [foot addSubview:btn];
        return foot;
    }
}
- (void)addBtn:(NSInteger)page {
    self.page += 1;
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"methodName=MallHomepageList&version=4.3&page=%ld&size=10&_time=1474439960&_signature=21e725800bb35fee0e1ce9428c773bfd", self.page];
    NSDictionary *dicHeader = @{@"Host" : @"api.izhangchu.com", @"Rferer" : @"http://h5.izhangchu.com/market/index.html?token=88E92FD7679C76FA91A54FE14641AD60&user_id=1666223&from=app&", @"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *arrData = dicData[@"data"];
        self.counts = dicData[@"total"];
        for (NSDictionary *dicAll in arrData) {
            [self.arrShoping addObject:[HXHotShopingModel ModelWithDic:dicAll]];
        }
        [_collectionView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0 || section == 1) {
        return CGSizeMake(0, 0);
    } else {
        return CGSizeMake(0, 50);
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    if (section == 0 || section == 1) {
        return CGSizeMake(0, 0);
    } else {
        return CGSizeMake(0, 50);
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HXShopingCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kShoping forIndexPath:indexPath];
        cell.backgroundColor = COLORAUTO;
        HXTopBannerModel *model = self.topBanner[indexPath.row];
        [cell.imageHeader sd_setImageWithURL:[NSURL URLWithString:model.bannerPicture]];
        return cell;
    } else if (indexPath.section == 1) {
        HXCycleCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCycleView forIndexPath:indexPath];
        cell.arrCycleSource = self.scrollBanner;
        cell.backgroundColor = COLORAUTO;
        return cell;
    } else {
        HXHotWaresCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHotWares forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        cell.hotModel = self.arrShoping[indexPath.row];
        return cell;
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(self.collectionView.HX_width, 120);
    } else if (indexPath.section == 1) {
        return CGSizeMake(self.collectionView.HX_width, 120);
    } else {
        return CGSizeMake((self.collectionView.HX_width - 2) / 2 , self.collectionView.HX_height * 0.45);
    }
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 2;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        HXTopBannerModel *topBanner = self.topBanner[indexPath.section];
        HXShopingTopVC *shoping = [[HXShopingTopVC alloc] init];
        shoping.netId = [NSString stringWithFormat:@"%ld", topBanner.typeId];
        [self.navigationController pushViewController:shoping animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
